package org.linlinjava.litemall.wx.web;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallMoneyLog;
import org.linlinjava.litemall.db.domain.LitemallTiXian;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallMoneyLogService;
import org.linlinjava.litemall.db.service.LitemallTiXianService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/moneyLog")
@Validated
public class WxTiXianController {

    @Autowired
   private LitemallTiXianService litemallTiXianService;

@Autowired
private LitemallUserService litemallUserService;

@Autowired
private   LitemallMoneyLogService litemallMoneyLogService;

    @GetMapping("/list")
    public Object listwtx(@LoginUser Integer userId,@RequestParam(value = "page",required = false,defaultValue = "1") int  page) {


        return ResponseUtil.ok(litemallTiXianService.userselectall(userId,page));
    }

    @PostMapping("/craete")
    public Object create(@LoginUser Integer userId, @RequestBody LitemallTiXian litemallTiXian) {

        LitemallUser user = litemallUserService.findById(userId);
        //限制金额
        if(user==null){ return ResponseUtil.fail(502,"登录数据异常");}
        if(litemallTiXian.getTixianjin()<100){return ResponseUtil.fail(502,"提现金额不得小于100");}
double  myjlj=user.getJianglijin();
double  txj=litemallTiXian.getTixianjin();
        if(myjlj >0 && myjlj>=txj){

            litemallTiXian.setUsername(user.getUsername());
        litemallTiXian.setUserid(user.getId());
        litemallTiXian.setLv(user.getUserLevel());
          int b=  litemallTiXianService.inserttx(litemallTiXian);

          if(b>0){

              litemallUserService.MoneyProdown(userId,txj,6);

              litemallMoneyLogService.insertMoney(txj,0,"申请提现",userId,"奖励金",0,user.getJianglijin()-txj,0);

          }


            return ResponseUtil.ok(b);
        }else {
            return ResponseUtil.fail(402,"余额不足");
        }


    }
    @GetMapping("/myresult")
    public Object myresult(@LoginUser Integer userId) {
       String result= litemallTiXianService.myresult(userId);
        return ResponseUtil.ok(result!=null?result:"0.00");
    }


}
